package cc.sends;

import java.util.Scanner;

/**
 * @ Description :
 * @ Author : ChristosC
 * @ Date : 2020/12/8 19:42
 */
public class Main {
    private static int[] freePartition = {32,16,64,64,16};
    private static int[] processNeed = {7,5,18,22,46};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Choose an algorithm: 1.FirstPartition 2.CycleFirstPartition 3.BestPartition 4.WorstPartition");
            switch (scanner.nextInt()){
                case 1:
                    FirstPartition();
                    break;
                case 2:
                    CycleFirstPartition();
                    break;
                case 3:
                    BestPartition();
                    break;
                case 4:
                    WorstPartition();
                    break;
                default:
                    System.out.println("Please input a right number!\n");
            }
    }

    /**
    * @description: FirstPartition Algorithm
    * @param：[]
    * @return void
    */
    public static void FirstPartition(){
        for (int i = 0; i < processNeed.length; i++) {
            for (int j = 0; j < freePartition.length; j++) {
                if (processNeed[i] <= freePartition[j]){
                    System.out.println("Process" + i + " is running on "+"Memory"+j);
                    freePartition[j] = freePartition[j] - processNeed[i];
                    processNeed[i] = 0;
                    break;
                }
            }
            if (processNeed[i] != 0){
                System.out.println("out!");
                return;
            }
        }
        System.out.println("");
    }

    public static void CycleFirstPartition(){
        int nextPoint = 0;
        int j = 0;
        boolean isWhile;
        for (int i = 0; i < processNeed.length; i++) {
            isWhile = true;
            while (isWhile){
                if (processNeed[i] <= freePartition[nextPoint]){
                    System.out.println("Process" + i + " is running on "  + "Memory" + nextPoint);
                    freePartition[nextPoint] = freePartition[nextPoint] - processNeed[i];
                    nextPoint++;
                    if (nextPoint > freePartition.length - 1){
                        nextPoint = 0;
                    }
                    isWhile = false;
                } else {
                    nextPoint++;
                    if (nextPoint > freePartition.length - 1){
                        nextPoint = 0;
                    }
                    j++;

                    if (j>=freePartition.length){
                        isWhile = false;
                        System.out.println("out!");
                    }
                }
            }
        }
        System.out.println("");
    }

    public static void BestPartition(){
        for(int i = 0; i < processNeed.length; i++) {
            for (int j = 0; j < freePartition.length; j++) {
                for (int n = 0; n < freePartition.length; n++) {
                    for (int m = n + 1; m < freePartition.length; m++) {
                        if (freePartition[n] > freePartition[m]) {
                            int temp;
                            temp = freePartition[n];
                            freePartition[n] = freePartition[m];
                            freePartition[m] = temp;
                        }
                    }
                }
                if (processNeed[i] <= freePartition[j]) {
                    System.out.println("process" + i + " is running on " + "Memory" + j);
                    freePartition[j] = freePartition[j] - processNeed[i];
                    processNeed[i] = 0;
                    break;
                }
            }
            if (processNeed[i] != 0) {
                System.out.println("out!");
                return;
            }
        }
        System.out.println("");
    }

    public static void WorstPartition(){
        for(int i = 0; i < processNeed.length; i++) {
            for (int j = 0; j < freePartition.length; j++) {
                for (int n = 0; n < freePartition.length; n++) {
                    for (int m = n + 1; m < freePartition.length; m++) {
                        if (freePartition[n] < freePartition[m]) {
                            int temp;
                            temp = freePartition[n];
                            freePartition[n] = freePartition[m];
                            freePartition[m] = temp;
                        }
                    }
                }
                if (processNeed[i] <= freePartition[j]) {
                    System.out.println("process" + i + " is running on " + "Memory" + j);
                    freePartition[j] = freePartition[j] - processNeed[i];
                    processNeed[i] = 0;
                    break;
                }
            }
            if (processNeed[i] != 0) {
                System.out.println("out!");
                return;
            }
        }
        System.out.println("");
    }

}
